﻿using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using ThresholdAssistant.DataContract.Models;
using System;
using System.Collections.Generic;
using System.Text.Json.Serialization;

namespace ThresholdAssistant.DataContract
{
    /// <summary>
    /// 消息内容标准格式
    /// </summary>
    /// <typeparam name="MessageTyep"></typeparam>
    [Serializable]
    public class MessageBody<MessageTyep> : MessageBody where MessageTyep : Enum
    {
        /// <summary>
        /// 消息类型
        /// </summary>
        [JsonProperty("type"), JsonPropertyName("type")]
        [Newtonsoft.Json.JsonConverter(typeof(StringEnumConverter))]
        public new MessageTyep Type { get; set; }
    }

    /// <summary>
    /// 消息内容标准格式
    /// </summary>
    [Serializable]
    public class MessageBody
    {
        /// <summary>
        /// 消息类型
        /// </summary>
        [JsonProperty("type"), JsonPropertyName("type")]
        public object Type { get; set; }

        /// <summary>
        /// 业务流水号
        /// </summary>
        [JsonProperty("serial_no"), JsonPropertyName("serial_no")]
        public string SerialNo { get; set; }

        /// <summary>
        /// 消息原始值
        /// </summary>
        [JsonProperty("value"), JsonPropertyName("value")]
        public string Value { get; set; }

        /// <summary>
        /// 数据详情
        /// </summary>
        [JsonProperty("data"), JsonPropertyName("data")]
        public object Data { get; set; }

        /// <summary>
        /// 时间戳
        /// </summary>
        [JsonProperty("time_stamp"), JsonPropertyName("time_stamp")]
        public long TimeStamp { get; set; }

        /// <summary>
        /// 请求头信息
        /// </summary>
        [JsonProperty("headers"), JsonPropertyName("headers")]
        public Dictionary<string, string> Headers { get; set; }

        /// <summary>
        /// 机器人信息
        /// </summary>
        [JsonProperty("robot_info"), JsonPropertyName("robot_info")]
        public RobotInfoModel RobotInfo { get; set; }

        /// <summary>
        /// 商家信息
        /// </summary>
        [JsonProperty("merchant_info"), JsonPropertyName("merchant_info")]
        public MerchantRobotInfoModel MerchantInfo { get; set; }

        /// <summary>
        /// 机器人授权的好友信息
        /// </summary>
        [JsonProperty("friends_info"), JsonPropertyName("friends_info")]
        public RobotFriendsInfoModel FriendsInfo { get; set; }

        /// <summary>
        /// 机器人授权的群信息
        /// </summary>
        [JsonProperty("chat_room_info"), JsonPropertyName("chat_room_info")]
        public RobotChatRoomInfoModel ChatRoomInfo { get; set; }

        /// <summary>
        /// 获取消息内容
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public T GetData<T>()
        {
            T result = default;
            if (Data == null)
                return result;

            var data = Data.ToString();
            if (string.IsNullOrEmpty(data))
                return result;

            return JsonConvert.DeserializeObject<T>(data);
        }
    }
}